package com.zhuss.opus.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.SearchKeyWordBo;
import com.zhuss.bean.bo.opus.opus.SearchOpusBo;
import com.zhuss.bean.bo.opus.opus.wechat.ListOpusBo;
import com.zhuss.bean.vo.DetailKeyWordVo;
import com.zhuss.bean.vo.opus.opus.DetailOpusVo;
import com.zhuss.bean.vo.opus.opus.ListOpusVo;
import com.zhuss.opus.db.dao.ext.OpusDao;
import com.zhuss.opus.db.entity.Opus;
import com.zhuss.common.PageVo;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;


/**
 * 查询作品 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("opusRead")
public class OpusRead {
	
	@Autowired
	private OpusDao opusDao;
	
	/**
	 * 作品 
	 * @return
	 */
	public Object listOpusId(Integer area) {
		List<Opus> opusList = opusDao.listAll(area);
		List<Long> result = new ArrayList<>();
		transferListId(opusList, result);
		return result;
	}
	
	
	/**
	 * keyWord
	 * @param bo
	 * @return
	 */
	public PageVo<DetailKeyWordVo> keyWord(SearchKeyWordBo bo) {
		PageVo<DetailKeyWordVo> result = new PageVo<DetailKeyWordVo>();
		List<DetailKeyWordVo> list = new ArrayList<DetailKeyWordVo>();
		list = opusDao.keyWord(bo);
		Integer count = opusDao.countKeyWord(bo);
		result.setPageNum(bo.getPageNum());
		result.setPageSize(bo.getPageSize());
		result.setTotal(count);
		result.setItems(list);
		return result;
	}
	
	
	
	/**
	 * 作品列表
	 * @param bo
	 * @return
	 */
	public Object listOpus(ListOpusBo bo) {
		PageVo<DetailOpusVo> result = new PageVo<DetailOpusVo>();
		List<DetailOpusVo> list = new ArrayList<DetailOpusVo>();
		list = opusDao.listOpus(bo);
		Integer count = opusDao.countOpusList(bo);
		result.setPageNum(bo.getPageNum());
		result.setPageSize(bo.getPageSize());
		result.setTotal(count);
		result.setItems(list);
		return result;
	}
	
	private void transferListId(List<Opus> opusList, List<Long> result) {
		for (Opus item : opusList) {
			result.add(item.getOpusId());
		}
	}

	/**
	 * 作品列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchOpusBo bo) {
		PageVo<ListOpusVo> result = new PageVo<ListOpusVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(opusDao.list(bo));
        result.setTotal(opusDao.countList(bo));
		return result;
	}
	
	/**
	 * 作品详情
	 * @param opusId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long opusId) throws WpsException {
		Object result = opusDao.detail(opusId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "opusId: " + String.valueOf(opusId));
		}
		return result;
	}
	
	

}
